import { _decorator, Component, Node, v3, v2, Vec3 } from 'cc';
import { GlobalEnum } from '../../../Init/Config/GlobalEnum';
import GlobalPool from '../../../Init/Tools/GlobalPool';
import { MappingObj } from './MappingObj';
const { ccclass, property } = _decorator;

@ccclass('MappingMissile')
export class MappingMissile extends MappingObj {
    lineV3 = v3();
    prePos = v3();
    initPos = v3();
    toPos = v3();
    time = 0;
    curt = 0;
    init(d: { p: Vec3, toP: Vec3, t: number }) {
        this.initPos.set(d.p);
        this.toPos.set(d.toP);
        this.time = d.t;
        this.curt = 0;

        this.node.setWorldPosition(this.initPos);
        this.prePos.set(this.node.worldPosition);

        //计算旋转
        this.lineV3.set(this.toPos).subtract(this.initPos).multiplyScalar(1 / d.t);
        let r = Math.atan2(-this.lineV3.z, this.lineV3.x);
        this.node.eulerAngles = this.tmpR.set(0, r * 57.3 + 90, 0);

    }

    update(dt) {
        this.curt += dt;
        if (this.curt >= this.time) {
            GlobalPool.put(this.node);
            return;
        }
        //位置
        this.tmpP.set(this.lineV3).multiplyScalar(dt);
        this.tmpP.add(this.node.worldPosition);
        this.node.setWorldPosition(this.tmpP);

    }

}

